package cn.edu.pku.service;

import cn.edu.pku.entity.CountInfo;
import cn.edu.pku.mapper.CountInfoMapper;
import cn.edu.pku.vo.CountInfoVo;
import cn.edu.pku.vo.CountResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by ming.qin on 2017/5/7.
 */
@Service
public class CountInfoService {

    @Autowired
    CountInfoMapper countInfoMapper;

    public int selectBookInfoCount() {
        return countInfoMapper.selectBookInfoCount();
    }

    public int selectBookNoteCount() {
        return countInfoMapper.selectBookNoteCount();
    }

    public List<CountInfoVo> selectCountInfo() {
        Calendar c = Calendar.getInstance();
        int currM = c.get(Calendar.MONTH) + 1;
        List<CountInfoVo> result = new ArrayList<>();
        List<CountInfo> list = countInfoMapper.selectCountInfo();
        Map<String, List<Integer>> tmp = new HashMap<>();
        for (CountInfo info : list) {
            String cate = info.getCate();
            if (tmp.containsKey(cate)) {
                while (tmp.get(cate).size() < info.getMonth() - 1) {
                    tmp.get(cate).add(0);
                }
                tmp.get(cate).add(info.getCount());
            } else {
                List<Integer> volist = new ArrayList<>();
                while (volist.size() < info.getMonth() - 1) {
                    volist.add(0);
                }
                volist.add(info.getCount());
                tmp.put(cate, volist);
            }
        }
        for (Map.Entry<String, List<Integer>> entry : tmp.entrySet()) {
            List<Integer> mlist = entry.getValue();
            while(mlist.size()<currM){
                mlist.add(0);
            }
            CountInfoVo vo = new CountInfoVo(entry.getKey(), mlist);
            result.add(vo);
        }
        return result;
    }

    public List<Map<String, Object>> selectBookCount() {
        return countInfoMapper.selectBookCount();
    }

    public List<Map<String, Object>> selectBorrowInfoCount() {
        return countInfoMapper.selectBorrowInfoCount();
    }

    public List<Map<String, Object>> selectBorrowerCount() {
        return countInfoMapper.selectBorrowerCount();
    }
}
